package com.stations.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.util.Base64;

/**
 * RSAUtils 加密工具类
 *
 * @author bh_ljy
 * @version 2023/10/27 15:59
 **/
public class RSAUtils {
    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIj2OFyQRCo3ZKQAUJOjx7ApLilP3bW9RgEfufPOFNm9DnjwcPJqJlUpGQLnyVQzbBBVBIMrMaL9AE3ET1mH4TUXmc+8zOI2t9QZ+Z+vpU+I6aKYzu3tzzESHfmbTXttaUiOtSmEDGR3/BLNXvmGvZR9zrTF+tnWkEbdaDNjQyrQIDAQAB";

    /**
     * 用公钥加密
     * @param data
     * @return
     */
    public static String encrypt(String data) {
        RSA rsa = new RSA(null, publicKey);
        byte[] encrypt2 = rsa.encrypt(StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        return Base64.getEncoder().encodeToString(encrypt2);
    }

    /**
     * 公钥解密
     * @param base64data
     * @return
     */
    public static String decrypt(String base64data) {
        RSA rsa = new RSA(null, publicKey);
        byte[] decrypt2 = rsa.decrypt(Base64.getDecoder().decode(base64data), KeyType.PublicKey);
        System.out.println(new String(decrypt2));
        return new String(decrypt2);
    }

}
